package mobidever.mygodutch.database.sqlitedal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import mobidever.mygodutch.R;
import mobidever.mygodutch.database.base.SQLiteDALBase;
import mobidever.mygodutch.model.ModelUser;
import mobidever.mygodutch.utility.DateTools;

public class SQLiteDALUser extends SQLiteDALBase
{

	public SQLiteDALUser(Context pContext)
	{
		super(pContext);
	}

	public boolean insertUser(ModelUser pModelUser)
	{
		ContentValues _ContentValues = modelUserChangeToContentValues(pModelUser);
		long _ID = getDatabase().insert(getTableNameAndPK()[0], null,
				_ContentValues);
		pModelUser.setUserID(_ID);
		return _ID >= 0;
	}

	public boolean deleteUser(String pCondition)
	{
		return getDatabase().delete(getTableNameAndPK()[0], pCondition, null) > 0;
	}

	public boolean updateUser(ModelUser pModelUser)
	{
		long _id = pModelUser.getUserID();
		ContentValues _ContentValues = modelUserChangeToContentValues(pModelUser);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				"UserID=" + _id, null) > 0;
	}

	public boolean updateUser(ModelUser pModelUser, String pCondition)
	{
		ContentValues _ContentValues = modelUserChangeToContentValues(pModelUser);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				"and UserID=" + pCondition, null) > 0;
	}

	public List<ModelUser> getUsers(String pCondtition)
	{
		String _sql = "select * from " + getTableNameAndPK()[0] + " where 1=1 "
				+ pCondtition;
		ArrayList<ModelUser> _ArrayList = (ArrayList<ModelUser>) getList(_sql);
		return _ArrayList;
	}

	private ContentValues modelUserChangeToContentValues(ModelUser pModelUser)
	{
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("UserName", pModelUser.getUserName());
		_ContentValues.put("CreateDate", DateTools.getFormatDateTime(
				pModelUser.getCreateDate(), "yyyy-mm-dd hh:mm:ss"));
		_ContentValues.put("State", pModelUser.getState());
		return _ContentValues;
	}

	private void createDefaultUser(SQLiteDatabase pDatabase)
	{
		String[] _UserName = getContext().getResources().getStringArray(
				R.array.InitDefaultUserName);
		for (int i = 0; i < _UserName.length; i++)
		{
			ModelUser _ModelUser = new ModelUser();
			_ModelUser.setUserName(_UserName[i]);
			ContentValues _ContentValues = modelUserChangeToContentValues(_ModelUser);
			long _ID = pDatabase.insert(getTableNameAndPK()[0], null,
					_ContentValues);
			_ModelUser.setUserID(_ID);
		}
	}

	@Override
	protected String[] getTableNameAndPK()
	{
		return new String[]
		{ "User", "UserID" };
	}

	@Override
	protected Object findModel(Cursor pCursor)
	{
		ModelUser _ModelUser = new ModelUser();
		_ModelUser.setUserName(pCursor.getString(pCursor
				.getColumnIndex("UserName")));
		_ModelUser.setUserID(pCursor.getInt(pCursor.getColumnIndex("UserID")));
		Date _Date = DateTools.getDate(
				pCursor.getString(pCursor.getColumnIndex("CreateDate")),
				"yyyy-MM-dd HH:mm:ss");
		_ModelUser.setCreateDate(_Date);
		_ModelUser.setState(pCursor.getInt(pCursor.getColumnIndex("State")));
		return _ModelUser;
	}

	public void onCreate(SQLiteDatabase pDatabase)
	{
		StringBuilder sCreateTableScript = new StringBuilder();

		sCreateTableScript.append("		Create  TABLE User(");
		sCreateTableScript
				.append("				[UserID] integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		sCreateTableScript.append("				,[UserName] varchar(10) NOT NULL");
		sCreateTableScript.append("				,[CreateDate] datetime NOT NULL");
		sCreateTableScript.append("				,[State] integer NOT NULL");
		sCreateTableScript.append("				)");
		pDatabase.execSQL(sCreateTableScript.toString());
		createDefaultUser(pDatabase);
	}

	public void onUpdate(SQLiteDatabase pDatabase)
	{
		// TODO Auto-generated method stub

	}

}
